//
//  DiscoveryController.m
//  soccer
//
//  Created by 上官千城 on 15/9/7.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "ODiscoveryController.h"

@interface ODiscoveryController ()

@end

@implementation ODiscoveryController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationItem.title = @"发现";
    
    UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    view.image = [UIImage imageNamed:@"圈子占位符@2x"];
    [self.view addSubview:view];
    
//创建tableview
    self.discoverTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.bounds.size.height) style:UITableViewStyleGrouped];
//    self.discoverTab.backgroundColor = [UIColor whiteColor];
    self.discoverTab.delegate = self;
    self.discoverTab.dataSource = self;
    [self.view addSubview:self.discoverTab];
    
    
    [self getMessage];
//创建头部视图
    self.heardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT *1/3)];
    self.heardView.backgroundColor = [UIColor whiteColor];
    self.discoverTab.tableHeaderView = self.heardView;
    self.heardScrool = [[OMyScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.heardView.frame.size.height)];
    self.heardScrool.backgroundColor = [UIColor whiteColor];
    [self.heardView addSubview:self.heardScrool];
    
   
    
    
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        
        return 10;
    } else {
    
        return 30;
    }

}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return @"您可能喜欢的";
    } else {
    
        return @"最新资讯";
    }


}

-(void)getMessage
{
    self.myArray = [NSMutableArray array];

    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
  
    NSString *url_string = [NSString stringWithFormat:@"%@",@"http://lolbox.oss.aliyuncs.com/json/v3/news/banner.json"];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableArray *array = responseObject;
        
            for (NSMutableDictionary *dic in array) {
                self.scroll = [[OScrollModel alloc] init];
                self.scroll.img = [dic objectForKey:@"img"];
                self.scroll.myId = [dic objectForKey:@"id"];
                [self.myArray addObject:self.scroll];
        
        
            }
//        NSLog(@"---------------myArray == %@",self.myArray);
            if (self.myArray.count != 0) {
                
                [self.heardScrool setImages:self.myArray];
            }

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];

    

}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        static NSString *scrollId = @"scrollCell";
        ODiscoverCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:scrollId];
        if (cell == nil) {
            
            cell = [[ODiscoverCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:scrollId];
            
        }
        
        NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"team1@2x.png",@"未标题-1_03@2x.png",@"未标题-1_06@2x.png",@"未标题-1_09@2x.png",@"未标题-1_22@2x.png",@"501@2x.png",@"506@2x.png",@"513@2x.png", nil];
        
        cell.interestImage = arr;
        return cell;
    } else if (indexPath.section == 0 && indexPath.row == 1) {
    
    static NSString *buttonId = @"buttonCell";
        ODiscoverCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:buttonId];
        if (cell == nil) {
            
            cell = [[ODiscoverCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:buttonId];
            
        }

    
        return cell;
    }
    
    
static NSString *identifier = @"discoverCell";
    ODiscoverCell *discoCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (discoCell == nil) {
        discoCell = [[ODiscoverCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
//    discoCell.backgroundColor = [UIColor whiteColor];
//    discoCell.textLabel.text = @"1111";
    
    return discoCell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 2;
    }else {
    
        return 20;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 80;
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        return 160;
    }
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];


}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
